/**
 *
 * @author Captain Normality
 */
package xmlparser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.vansante.EVEMap.Main;
import net.vansante.EVEMap.Data.*;

public class OrderHandler 
{
    private List<BuySellOrder> orders;
    private double highPrice = 0.0;
    private double lowPrice = Double.POSITIVE_INFINITY; 
    private double totalPrice = 0.0;
    private double medianValue = Double.NaN;
    private int numOrdersAtLastMedianCalculation = -1;
    
    public OrderHandler()
    {
        orders = new ArrayList<>();
    }
    
    public List<BuySellOrder> getOrders()
    {
        return Collections.unmodifiableList(orders);
    }
    
    public void addOrder(BuySellOrder newOrder)
    {
        orders.add(newOrder);
        
        if(newOrder.price > highPrice)
        {
            highPrice = newOrder.price;
        }
        if(newOrder.price < lowPrice)
        {
            lowPrice = newOrder.price;
        }
        
        totalPrice += newOrder.price;
    }
    
    public double getHighPrice()
    {
        return highPrice;
    }
    
    public double getLowPrice()
    {
        return lowPrice;
    }
    
    public double getAvgPrice()
    {
        return totalPrice/orders.size();
    }  
    
    public double getMedianPrice(){
        if(orders.size() != numOrdersAtLastMedianCalculation && orders.size() > 0)
        {
            Collections.sort(orders);
            int middleIndex = orders.size()/2; //assume odd
            
            if (orders.size()%2 != 0) //odd
                return orders.get(middleIndex).price;
            else { //even
                medianValue = (orders.get(middleIndex).price + orders.get(middleIndex+1).price) / 2;
            }
        }
        return medianValue;
    }
}
